package com.css.zfzx.fwsjcj.modules.expert.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.fwsjcj.modules.expert.entity.FwExpertEntity;
import com.css.zfzx.fwsjcj.modules.expert.entity.FwUserDivisionEntity;
import com.css.zfzx.fwsjcj.modules.expert.entity.FwUserEntity;
import com.css.zfzx.fwsjcj.modules.expert.respository.DivisionRespository;
import com.css.zfzx.fwsjcj.modules.expert.respository.FwExpertQueryRespository;
import com.css.zfzx.fwsjcj.modules.expert.respository.FwExpertRespository;
import com.css.zfzx.fwsjcj.modules.expert.respository.FwUserDivisionRespository;
import com.css.zfzx.fwsjcj.modules.expert.service.FwExpertService;
import com.css.zfzx.fwsjcj.modules.expert.vo.ExpertVo;
import com.css.zfzx.fwsjcj.modules.expert.vo.FwUserDivisionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author
 * @program
 * @description
 * @create 2021/12/22 9:19
 **/
@Service
public class FwExpertServiceImpl implements FwExpertService {

    @Autowired
    private FwExpertQueryRespository fwExpertQueryRespository;

    @Autowired
    private FwExpertRespository fwExpertRespository;

    @Autowired
    private DivisionRespository divisionRespository;

    @Autowired
    private FwUserDivisionRespository fwUserDivisionRespository;


    @Override
    public JSONObject getPage(HttpServletRequest request, int curPage, int pageSize, ExpertVo expertVo) {
        Page<FwUserEntity> page = fwExpertQueryRespository.getPage(request, curPage, pageSize, expertVo);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(page);
        return jsonObject;
    }

    @Override
    public void save(ExpertVo expertVo) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        FwExpertEntity fwExpertEntity=new FwExpertEntity();
        fwExpertEntity.setUserId(expertVo.getUserId());
        fwExpertEntity.setStructureType(expertVo.getStructureType());
        fwExpertEntity.setDataType(expertVo.getDataType());
        fwExpertEntity.setId(UUIDGenerator.getUUID());
        fwExpertEntity.setCreateUser(user.getUserId());
        fwExpertEntity.setCreateTime(new Date());


        List<FwUserDivisionVo> divisions = expertVo.getDivisions();
        FwUserDivisionEntity fwUserDivisionEntity=new FwUserDivisionEntity();
        fwUserDivisionEntity.setUserId(expertVo.getUserId());
        fwUserDivisionEntity.setCreateUser(user.getUserId());
        fwUserDivisionEntity.setCreateTime(new Date());
        for (FwUserDivisionVo fwUserDivisionVo : divisions) {
            fwUserDivisionEntity.setId(UUIDGenerator.getUUID());
            System.out.println(fwUserDivisionVo);
            BeanUtils.copyProperties(fwUserDivisionVo,fwUserDivisionEntity);
            fwUserDivisionRespository.save(fwUserDivisionEntity);
        }
        fwExpertRespository.save(fwExpertEntity);
    }

    @Override
    public void edit(ExpertVo expertVo) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        FwExpertEntity fwExpertEntity=new FwExpertEntity();
        fwExpertEntity.setUserId(expertVo.getUserId());
        fwExpertEntity.setStructureType(expertVo.getStructureType());
        fwExpertEntity.setDataType(expertVo.getDataType());
        fwExpertEntity.setUpdateUser(user.getUserId());
        fwExpertEntity.setUpdateTime(new Date());
        fwUserDivisionRespository.del(expertVo.getUserId());
        List<FwUserDivisionVo> divisions = expertVo.getDivisions();
        FwUserDivisionEntity fwUserDivisionEntity=new FwUserDivisionEntity();
        fwUserDivisionEntity.setUserId(expertVo.getUserId());
        fwUserDivisionEntity.setCreateUser(user.getUserId());
        fwUserDivisionEntity.setCreateTime(new Date());
        for (FwUserDivisionVo fwUserDivisionVo : divisions) {
            fwUserDivisionEntity.setId(UUIDGenerator.getUUID());
            System.out.println(fwUserDivisionVo);
            BeanUtils.copyProperties(fwUserDivisionVo,fwUserDivisionEntity);
            fwUserDivisionRespository.save(fwUserDivisionEntity);
        }
        fwExpertRespository.edit(fwExpertEntity);
    }

    @Override
    public  List<DivisionEntity> initZtree() {
        List<DivisionEntity> ztreeNodes=divisionRespository.initZtree();
        return ztreeNodes;
    }

    @Override
    public String getExpertStructureType(String userId) {
        String StructureType=divisionRespository.getExpertStructureType(userId);
        return StructureType;
    }

    @Override
    public List<Map<String,Object>> getProvince(String userId) {
        List<Map<String,Object>> provinceList=fwUserDivisionRespository.getProvince(userId);
        return provinceList;
    }

    @Override
    public List<Map<String, Object>> getCity(String userId) {
        List<Map<String,Object>> cityList=fwUserDivisionRespository.getCity(userId);
        return cityList;
    }

    @Override
    public List<Map<String,Object>> getProvincialCity(String userId, String provinceId) {
        List<Map<String,Object>> cityList=fwUserDivisionRespository.getProvincialCity(userId,provinceId);
        return cityList;
    }

    @Override
    public String getExpertDataType(String userId) {
        String dataType=divisionRespository.getExpertDataType(userId);
        return dataType;
    }

    @Override
    public List<FwUserDivisionEntity> getDataRange(String userId) {
        List<FwUserDivisionEntity> dataRange=fwUserDivisionRespository.getDataRange(userId);
        return dataRange;
    }

    @Override
    public Map<String, Object> getProvincialCityName(String userId) {
        Map<String, Object> map=new HashMap<>();
        List<Map<String, Object>> province = getProvince(userId);
        List<Map<String, Object>> city = getCity(userId);
        String structureType = getExpertStructureType(userId);
        map.put("province",province);
        map.put("city",city);
        map.put("structureType",structureType);
        return map;
    }


}
